﻿Imports DTO
Imports BUS
Imports System.Data.OleDb

Public Class TiepNhanSach

    Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub
    Public Sub LoadData()
        Dim strSQL As String = "SELECT  * FROM Sach,TheLoai,TinhTrang,NhanVien where Sach.MaNhanVien=NhanVien.MaNV and Sach.MaTinhTrang=TinhTrang.MaTinhTrang and Sach.MaTheLoai=TheLoai.MaTheLoai"
        Dim dataset As DataTable = New DataTable()

        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=QUANLYTHUVIEN.accdb")
        Dim cmd As OleDbCommand = New OleDbCommand(strSQL, con)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        ad.Fill(dataset)
        DataGridView_hosonhanvien.AutoGenerateColumns = False
        DataGridView_hosonhanvien.DataSource = dataset
        'Dim ds As List(Of SachDTO)
        'Dim bus As New SachBUS()
        'ds = bus.LaySach()
        'DataGridView_hosonhanvien.Rows.Clear()
        'For i As Integer = 0 To ds.Count - 1
        '    DataGridView_hosonhanvien.Rows.Add(ds(i).TenSach, ds(i).MaTheLoai, ds(i).TacGia, ds(i).NhaXuatBan, ds(i).NamXuatBan, ds(i).NgayNhap, ds(i).TriGia, ds(i).MaNhanVien, ds(i).KhoangCachXuatBan, ds(i).MaTinhTrang)
        'Next

    End Sub
    Private Sub TiepNhanSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'load BangCap Len combox'''''''''''''''''''''''''
        Dim theloai As New TheLoaiBUS()
        Dim dsach As New List(Of TheLoaiDTO)
        dsach = theloai.Laytheloai()
        ComboBox_theloai.DataSource = dsach
        ComboBox_theloai.DisplayMember = "TenTheLoai"
        ComboBox_theloai.ValueMember = "MaTheLoai"

        ''''''''load CHucVu len combox''''''''''''
        Dim tinhtrang As New TinhTrangBUSvb()
        Dim ds1 As New List(Of TinhTrangDTO)
        ds1 = tinhtrang.LayTinhTrang()
        ComboBox_tinhtrang.DataSource = ds1
        ComboBox_tinhtrang.DisplayMember = "TenTinhTrang"
        ComboBox_tinhtrang.ValueMember = "MaTinhTrang"


        '''''load  BoPhan len combox''''''''''''''

        Dim nhanvien As New NhanVienBUS()
        Dim ds2 As New List(Of NhanVienDTO)
        ds2 = nhanvien.LayNhanVien()
        ComboBox_nhanvien.DataSource = ds2
        ComboBox_nhanvien.DisplayMember = "HoTenNV"
        ComboBox_nhanvien.ValueMember = "MaNV"
        ComboBox_nhanvien.SelectedIndex = 0
        LoadData()

    End Sub

    Private Sub btnThem_hss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem_hss.Click
        Dim nvDTO As New SachDTO()
        'lay du lieu tu cac control 
        nvDTO.TenSach = txttensach.Text.Trim()
        nvDTO .TenSach =txttrigia .Text .Trim ()
        nvDTO.MaTheLoai = ComboBox_theloai.SelectedValue 'ComboBox_bangcap.Items(ComboBox_bangcap.SelectedIndex) 
        nvDTO.MaTinhTrang = Convert.ToInt32(ComboBox_tinhtrang.SelectedValue)
        nvDTO.MaNhanVien = Convert.ToInt32(ComboBox_nhanvien.SelectedValue)
        nvDTO.NhaXuatBan = txt_nhaxuatban.Text.Trim()
        nvDTO.NamXuatBan = DateTimePicker_namxuatban.Value.ToLongDateString()
        nvDTO.NgayNhap = DateTimePicker_ngaynhap.Value.ToLongDateString()
        'Dim strSQL As String = "insert into NhanVien(HoTen,MaBangCap,MaBoPhan,MaChucVu,DiaChi,DienThoai,NgaySinh) values('" + nvDTO.HoTen.ToString() + "'," + nvDTO.MaBangCap.ToString() + "," + nvDTO.MaBoPhan.ToString() + "," + nvDTO.MaChucVu.ToString() + ",'" + nvDTO.DiaChi.ToString() + "','" + nvDTO.DienThoai.ToString() + "','" + nvDTO.NgaySinh.ToString() + "')"
        ''goi BUS
        Dim kq As Integer
        Dim bus As New SachBUS()
        kq = bus.Themsach(nvDTO)
        LoadData()
        If kq = 1 Then
            MessageBox.Show("them thanh cong")
            LoadData()
        Else
            MessageBox.Show("them that bai")
        End If

    End Sub

    Private Sub btnxoa_hss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa_hss.Click
        Dim nvDTO As New SachDTO()
        'lay du lieu tu cac control 
        Dim dr As DialogResult = New DialogResult()
        dr = MessageBox.Show("Ban muon xoa?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)

        If dr = Windows.Forms.DialogResult.Yes Then
           
            Dim manv As Int32

            If DataGridView_hosonhanvien.CurrentRow.Cells("Col").Value.ToString() <> "" Then
                manv = Convert.ToInt32(DataGridView_hosonhanvien.CurrentRow.Cells("ColMaNV").Value.ToString())
            End If

            Dim kq As Boolean
            Dim bus As New NhanVienBUS()
            kq = bus.xoanhanvien(manv)
            If kq = True Then
                MessageBox.Show("xoa thanh cong")
                LoadData()
            Else
                MessageBox.Show("xoa that bai")
            End If
        End If
    End Sub

    Private Sub btnsua_hss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua_hss.Click
        Dim nvDTO As New SachDTO()
        'lay du lieu tu cac control 
        nvDTO.TenSach = txttensach.Text.Trim()
        nvDTO.TenSach = txttrigia.Text.Trim()
        nvDTO.MaTheLoai = ComboBox_theloai.SelectedValue 'ComboBox_bangcap.Items(ComboBox_bangcap.SelectedIndex) 
        nvDTO.MaTinhTrang = Convert.ToInt32(ComboBox_tinhtrang.SelectedValue)
        nvDTO.MaNhanVien = Convert.ToInt32(ComboBox_nhanvien.SelectedValue)
        nvDTO.NhaXuatBan = txt_nhaxuatban.Text.Trim()
        nvDTO.NamXuatBan = DateTimePicker_namxuatban.Value.ToLongDateString()
        nvDTO.NgayNhap = DateTimePicker_ngaynhap.Value.ToLongDateString()
        'goi(bus)

        Dim kq As Integer
        Dim bus As New SachBUS()
        kq = bus.updatesach(nvDTO)
    End Sub
End Class